/**
 * SpringMVC restful 风格
 * 商品管理控制器，控制关于商品的一些业务逻辑操作
 * @author zhangq
 * @date 2015-01-26
 */
package com.chnbst.bstyun.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.Order;
import com.chnbst.bstyun.model.pojo.StaticData;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.GoodsVo;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.OrderService;
import com.chnbst.bstyun.service.StaticDataService;
import com.chnbst.bstyun.service.UserService;

@Controller
@RequestMapping("/goods")
public class GoodsController {

	//每页显示的用户数
	private int pageSize = 20;
	//用户列表页码
	private int currPage = 1;
		
	/**
	 * 注入业务逻辑层
	 */
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private StaticDataService staticDataService;
	
	/**
	 * 用户商品 应用中心
	 * @param request
	 * @return 商品信息
	 */
	@RequestMapping(value="/applyCenter",method=RequestMethod.GET)
	public ModelAndView cloudmarket(HttpServletRequest request)throws Exception{
		ModelAndView mav = new ModelAndView();
		List<StaticData> industries = staticDataService.selectStaticDataByStaticDataCode("110000");
		List<StaticData> goodCategories = staticDataService.selectStaticDataByStaticDataCode("120000");
		mav.addObject("industries", industries);
		mav.addObject("goodCategories", goodCategories);
		mav.setViewName("/mall/applyCenter");
		return mav;
	}
	
	/**
	 * Ajax 分页查询发布商品list
	 * @return 商品列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/applyCenter/goodsList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goodsList(String page,HttpServletRequest request)throws Exception{
		User user = (User)request.getSession().getAttribute("userInfo");
		Map<String, Object> map = new HashMap<String, Object>();
		String industryCode = request.getParameter("industry");
		String categoryCode = request.getParameter("category");
		String goodsName = request.getParameter("goodsName");
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		map.put("industryCode", industryCode);
		map.put("categoryCode", categoryCode);
		map.put("goodsName", goodsName);
		map.put("isPublish", 1);
		int totalRows = goodsService.selectGoodsCount(map);
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "publish_time");
		map.put("sortOrder", "desc");
		List<GoodsVo> goodsList = goodsService.selectGoods(map);
		for(GoodsVo goods : goodsList){
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("orderState", 1);
			map1.put("userId", user.getUserId());
			map1.put("goodsId", goods.getGoodsId());
			
			List<Order> orderList = orderService.selectOrderListByGoodsId(map1);
			if(orderList.size() > 0){
				goods.setTypeNum(1);//用户已购买的类型状态设置为1
			}else {
				goods.setTypeNum(0);//用户未购买的类型状态设置为0
			}
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("currPage", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("goodsList", goodsList);
		return resultMap;
	}
}